//
//  DZDDetailVC.swift
//  SlaughterClient
//
//  Created by 邱仙凯 on 2021/9/27.
//  Copyright © 2021 zwb. All rights reserved.
//

import UIKit
 
class ZyaDZDDetailVC: UITableViewController,DateWidgetable {
   
    var pmonth = Date.preMonths(1)
    var type = ""
    var id_key = ""
   private var main = JoinSuperModel(JSON:["":""])!
     private var sMonth = Date.currentMonth
   private var sDate = Date.today
   var sectionCount = 1
   
   var helper = ZyaTenderHelper()
   lazy var header:ZyaDZDDetailHeader = {
       let v = ZyaDZDDetailHeader()
      v.frame = CGRect(x:0,y:0,width:KSGlobal.screenW,height:130)
        return v
   }()
   
   lazy var descLab:UILabel = {
       let v = UILabel()
       v.backgroundColor = .white
       v.textAlignment = .center
       v.frame = CGRect(x:0,y:0,width:KSGlobal.screenW,height:38)
       return v
   }()
   
   override func viewDidLoad() {
       super.viewDidLoad()

       self.title = "单据明细"
     
         self.tableView.register(ZyaDZDDetailCell.self, forCellReuseIdentifier: ZyaDZDDetailCell.identifier)
       
         self.tableView.estimatedRowHeight = 130
       self.tableView.rowHeight = UITableView.automaticDimension
       self.tableView.backgroundColor = Theme.awModuleBgColor
       self.tableView.tableHeaderView = header
       self.tableView.separatorStyle = .none
       self.tableView.tableFooterView = UIView()
       self.tableView.mj_header = KSRefreshHeader {
            self.network()
       }
    
       self.tableView.mj_header?.beginRefreshing()
       
       
   }


   
   override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
       tableView.deselectRow(at: indexPath, animated: true)
        let vc = ZyaDZDDetailVC()
        self.navigationController?.pushViewController(vc, animated: true)
       
   }
   
   override func numberOfSections(in tableView: UITableView) -> Int {
       return sectionCount
   }
   
   override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       if let arr = self.main.detailArray {
           return arr.count
        } else {
           return 0
        }
   }
   override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       guard let cell = tableView.dequeueReusableCell(withIdentifier: ZyaDZDDetailCell.identifier, for: indexPath) as? ZyaDZDDetailCell else {
           fatalError()
       }
       if let arr = self.main.detailArray {
           cell.arr = arr[indexPath.row].detail_joint
       }
       return cell
       
   }
  
 
 
   func network() {
       KSNetwork.shared.doRequest(API.supplierTableQuery(type: type, id_key: id_key),showHud: false) { (data:BaseResult<JoinSuperModel>) in
           if data.flag == TRUE {
               if let info = data.info_object {
                   self.main = info
                   self.header.arr = self.main.main_joint
                   self.header.ks_height = CGFloat(self.main.main_joint!.count * 26)
                   self.tableView.reloadData()
               }
           } else {
               KSToast(data.errorCombine.unwrap)
           }
           self.tableView.mj_header?.endRefreshing()
       }

   }
   
   
   
   
}
class ZyaDZDDetailHeader:UIView {
    lazy var bg:UIView = {
       let v = UIView()
        v.backgroundColor = .white
        v.cornerRadius = 5
        return v
    }()
 
   override init(frame: CGRect) {
       super.init(frame: frame)
       self.addSubview(bg)
       bg.snp.makeConstraints { make in
           make.left.top.equalTo(self).offset(5)
           make.right.bottom.equalTo(self).offset(-5)
        }
 
   }
    var lastLab:UILabel?
    var arr:[JoinModel]? {
        didSet {
            if let arr = arr {
                for subv in bg.subviews {
                    subv.removeFromSuperview()
                }
                self.lastLab = nil
                for i in 0..<arr.count {
                    let lab = UILabel()
                    lab.font = UIFont.systemFont(ofSize: 15)
                    lab.text = arr[i].name
                    
                    let right = UILabel()
                    right.font = UIFont.systemFont(ofSize: 15)
                     right.text = arr[i].value
                    bg.addSubview(lab)
                    bg.addSubview(right)
                    lab.snp.makeConstraints { make in
                        make.left.equalTo(bg).offset(10)
                        if lastLab.isSome {
                            make.top.equalTo(self.lastLab!.snp.bottom).offset(5)
                        } else {
                            make.top.equalTo(bg).offset(10)
                        }
                      }
                    right.snp.makeConstraints { make in
                        make.left.equalTo(bg).offset(100)
                             make.centerY.equalTo(lab)
                     }
                    self.lastLab = lab
                }
            }
        }
        
    }
   
   required init?(coder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
}
